package com.china08.yxyapi.api.manage.stu.mystudy.homework.dowork;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;

@CrossOrigin
@RestController
public class DoWorkTimeGetController {

    @Autowired
    private HomeworkTaskReportRepository htrr;

    /**
     * 查询到目前为止，任务的做题时长2017年3月22日16:38:55
     * 
     * @param id
     *            作业id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/stu/mystudy/homework/dowork/getTime", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(@RequestBody ReqModel reqModel, HttpServletRequest request,
            HttpServletResponse response) {
        RespModel respModel = new RespModel();

        HomeworkTaskReport homeworkTaskReport = htrr.findByStudentIdAndTaskId(reqModel.studentId, reqModel.taskId);
        Assert.notNull(homeworkTaskReport, "获取作业失败，请联系管理员！");
        respModel.time = homeworkTaskReport.getDuration();
        System.err.print("这里不应该被调用了");
        return respModel;
    }

    // 返回的对象类型
    protected class RespModel extends YxyApiRespModel {
        public Long time = 0l;
    }

    // 请求的参数类型
    protected static class ReqModel extends YxyApiReqModel {
        // 任务ID
        public String taskId;
        // 学生id
        public String studentId;
    }
}
